// PermissionManager.ts
export enum Permission {
  Camera = "scope.camera", //相机
  Location = "scope.userLocation", //定位
  LocationBackground = "scope.userLocationBackground", //后台定位
  Microphone = "scope.record", //麦克风
  Album = "scope.album", //相册
  Address = "scope.address", //通信地址
  WritePhotosAlbum = "scope.writePhotosAlbum", //保存到相册
  Record = "scope.record", //录音
  UserInfo = "scope.userInfo", //用户信息
}

class PermissionManager {
  static async requestPermission(permission: Permission): Promise<boolean> {
    return new Promise((resolve) => {
      if (process.env.UNI_PLATFORM === "mp-weixin") {
        // 微信小程序
        wx.getSetting({
          success: (res) => {
            if (res.authSetting[permission]) {
              resolve(true); // 已授权
            } else {
              wx.authorize({
                scope: permission,
                success: () => {
                  resolve(true); // 授权成功
                },
                fail: () => {
                  resolve(false); // 授权失败
                },
              });
            }
          },
          fail: () => {
            resolve(false); // 获取设置失败
          },
        });
      } else if (process.env.UNI_PLATFORM === "app-plus") {
        // App
        // 这里使用 uni.authorize 进行权限请求
        uni.authorize({
          scope: permission,
          success: () => {
            resolve(true); // 授权成功
          },
          fail: () => {
            resolve(false); // 授权失败
          },
        });
      } else {
        resolve(false); // 不支持其他平台
      }
    });
  }
}

export default PermissionManager;
